package com.dq.domain.position;

import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cf
 */
@Data
@Entity
@ApiModel("岗位")
public class OrgPosition extends BaseEntity implements VO.ToVO {

    @ApiModelProperty("岗位编号")
    protected String ordersNo;
    @NotBlank
    @ApiModelProperty("岗位名称")
    protected String postName;
    @ApiModelProperty("描述")
    @Column(columnDefinition="text")
    protected String depict;

    @ApiModelProperty("排序")
    @Column(columnDefinition="int default 1")
    protected Integer sortNo;

    @ManyToOne
    @ApiModelProperty(value = "父对象", hidden = true)
    protected OrgPosition parent;

    @Data
    @ApiModel(value = "岗位视图")
    public static class Vo implements VO {
        protected Long id;
        @ApiModelProperty(value = "岗位编号")
        private String ordersNo;
        @ApiModelProperty("岗位名称")
        protected String postName;
        @ApiModelProperty("描述")
        protected String depict;
        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "修改时间")
        private String lastModifyTime;
        @ApiModelProperty(value = "创建者id")
        private Long createUserId;
        @ApiModelProperty(value = "创建者name")
        private String createUserName;
        @ApiModelProperty(value = "修改者id")
        private Long updateUserId;
        @ApiModelProperty(value = "修改者name")
        private String updateUserName;

        @ApiModelProperty("排序")
        private Integer sortNo;
        @ApiModelProperty("子项")
        private List<OrgPosition.Vo> childs = new ArrayList<>();
        @ApiModelProperty("父级id")
        private Long parentId;
        @ApiModelProperty("父级岗位名称")
        private String parentPostName;

    }

    @Data
    @ApiModel("岗位列表展示")
    public static class OrgPositionListVo{
        public Long id;

        public String name;
    }

    public OrgPositionListVo toOrgPositionListVo (){
        OrgPosition.OrgPositionListVo orgPositionListVo = new OrgPosition.OrgPositionListVo();
        orgPositionListVo.id = this.getId();
        orgPositionListVo.name = fmt(this.getPostName());
        return orgPositionListVo;
    }
    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;
        vo.ordersNo = this.ordersNo;
        vo.postName = this.postName;
        vo.depict = this.depict;
        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.lastModifyTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);

        vo.createUserId = this.createUser == null ? null : this.createUser.getId();
        vo.createUserName = this.createUser == null ? null : this.createUser.getUsername();
        vo.updateUserId = this.updateUser == null ? null : this.updateUser.getId();
        vo.updateUserName = this.updateUser == null ? null : this.updateUser.getUsername();

        vo.sortNo = this.sortNo;

        vo.parentId = this.parent == null ? null : this.parent.getId();
        vo.parentPostName = this.parent == null ? "" : this.parent.getPostName();

        return vo;
    }

}
